package com.creditease.tmsp.modules.sys.service;


import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.creditease.tmsp.common.persistence.IdGenerator;
import com.creditease.tmsp.common.persistence.MyBatisBaseDao;
import com.creditease.tmsp.common.service.MyBatisBaseService;
import com.creditease.tmsp.modules.sys.dao.SysResourceDao;
import com.creditease.tmsp.modules.sys.entity.SysResource;

/**
 * 
 * @Description：资源Service
 * @author：
 * @CreateDate：2014年6月18日下午6:19:59 
 * @Company:
 * @version V1.0
 */

@Service
//@Transactional(readOnly = true)
public class SysResourceService extends MyBatisBaseService<SysResource>  {
	
	@Autowired
	private SysResourceDao sysResourceDao;

	@Override
	public MyBatisBaseDao<SysResource> getDao() {
		return sysResourceDao;
	}
	
	public List<SysResource> findAllMenu(){
		
		return sysResourceDao.findAllSysResource();
	}
	
	public void saveSysResource(SysResource sysResource) {
		
		sysResource.setParent(this.getById(sysResource.getParent().getId()));
		sysResource.setId(IdGenerator.getId());
		sysResource.setParentId(sysResource.getParent().getId());
		sysResource.setParentIds(sysResource.getParent().getParentIds()+sysResource.getParent().getId()+",");
		sysResource.setDelFlag("0");
		
		sysResourceDao.save(sysResource);
	}
	
	public void updateSysResource(SysResource sysResource) {
		sysResource.setParent(this.getById(sysResource.getParent().getId()));
		sysResource.setParentId(sysResource.getParent().getId());
		sysResource.setParentIds(sysResource.getParent().getParentIds()+sysResource.getParent().getId()+",");
		sysResource.setDelFlag("0");
		String oldParentIds = sysResource.getParentIds(); // 获取修改前的parentIds，用于更新子节点的parentIds
		
		//更新子节点 parentIds
		List<SysResource> list = sysResourceDao.findByParentIdsLike(sysResource.getId());
		for (SysResource e : list){
			e.setParentIds(e.getParentIds().replace(oldParentIds, sysResource.getParentIds()));
		}
		sysResourceDao.update(sysResource);
	}

	public List<SysResource> findByRoles(List<String> roles) {
		return sysResourceDao.findByRoles(roles);
	}
	
}
